package com.cxh.rmsq.utils.dialog

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.TextView
import com.blankj.utilcode.util.ToastUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.utils.TextSpanUtil
import com.hjq.shape.view.ShapeEditText
import com.hjq.shape.view.ShapeTextView
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.xuexiang.xui.utils.XToastUtils
import kotlin.math.roundToInt


class IntroducePopup(
    context: Context,
    content: String,
    click: onClick
) :
    BottomPopupView(context) {

    interface onClick {
        fun click(content: String, s1: String)
    }

    var func: onClick
    var content: String
    private val tv_num_listener: TextView by lazy { findViewById<TextView>(R.id.tv_num_listener) }
    private var temp: CharSequence? = null

    init {
        this.func = click
        this.content = content
    }

    override fun getImplLayoutId(): Int {
        return R.layout.introduce_popup
    }

    override fun onCreate() {
        super.onCreate()

        var tv_content = findViewById<ShapeEditText>(R.id.set_content)
        var stv_publish = findViewById<ShapeTextView>(R.id.stv_publish)
        tv_content.setText(content)
        tv_content.setSelection(tv_content.text.toString().length)
        tv_content.isCursorVisible = true
        tv_content.requestFocus()

        tv_content.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                tv_num_listener.text = "${temp!!.length}/50"
                TextSpanUtil.setSpan(
                    tv_num_listener,
                    tv_num_listener.text.toString(),
                    arrayOf("/50"),
                    R.color.color_999999
                )
                if (temp!!.length > 50) {
                    XToastUtils.toast("输入不能超过50个字符！")
                }

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                temp = s
            }

        })

        tv_num_listener.text = "${tv_content.text!!.length}/50"
        TextSpanUtil.setSpan(
            tv_num_listener,
            tv_num_listener.text.toString(),
            arrayOf("/50"),
            R.color.color_999999
        )
        stv_publish.setOnClickListener {
            if (TextUtils.isEmpty(tv_content.text.toString().trim())) {
                ToastUtils.showShort("请输入简介内容")
                return@setOnClickListener
            }
            func.click(
                tv_content.text.toString().trim(),
                "",
            )
            dismiss()
        }

    }

    // 最大高度为Window的0.85
    override fun getMaxHeight(): Int {
        return ((XPopupUtils.getAppHeight(context) * .95f).roundToInt())
    }

}